|
1
|
|
|
GLSR.shortcode.close = function( el ) |
|
|
|
|
|
|
2
|
|
|
{ |
|
3
|
|
|
var button = x(( el = el || '.glsr-mce-button' )); |
|
|
|
|
|
|
4
|
|
|
if( button.length ) { |
|
5
|
|
|
button.removeClass( 'active' ).parent().find( '.glsr-mce-menu' ).hide(); |
|
6
|
|
|
} |
|
7
|
|
|
}; |
|
8
|
|
|
|
|
9
|
|
|
GLSR.shortcode.open = function( el ) |
|
|
|
|
|
|
10
|
|
|
{ |
|
11
|
|
|
x( el ).addClass( 'active' ).parent().find( '.glsr-mce-menu' ).show(); |
|
12
|
|
|
}; |
|
13
|
|
|
|
|
14
|
|
|
GLSR.shortcode.toggle = function( ev ) |
|
|
|
|
|
|
15
|
|
|
{ |
|
16
|
|
|
ev.preventDefault(); |
|
17
|
|
|
if( x( this ).hasClass( 'active' ) ) { |
|
18
|
|
|
GLSR.shortcode.close( this ); |
|
|
|
|
|
|
19
|
|
|
} |
|
20
|
|
|
else { |
|
21
|
|
|
GLSR.shortcode.open( this ); |
|
22
|
|
|
} |
|
23
|
|
|
}; |
|
24
|
|
|
|
|
25
|
|
|
GLSR.shortcode.trigger = function( ev ) |
|
26
|
|
|
{ |
|
27
|
|
|
ev.preventDefault(); |
|
28
|
|
|
// GLSR.shortcode.current is used by scForm to trigger the correct popup |
|
29
|
|
|
GLSR.shortcode.current = x( this ).attr( 'data-shortcode' ); |
|
|
|
|
|
|
30
|
|
|
if( !GLSR.shortcode.current )return; |
|
|
|
|
|
|
31
|
|
|
if( !tinymce.get( window.wpActiveEditor ) ) { |
|
|
|
|
|
|
32
|
|
|
// Quicktags Editor |
|
33
|
|
|
if( !x( '#scTemp' ).length ) { |
|
34
|
|
|
x( 'body' ).append( '<textarea id="scTemp" style="display: none;" />' ); |
|
35
|
|
|
tinymce.init({ |
|
36
|
|
|
mode : 'exact', |
|
37
|
|
|
elements : 'scTemp', |
|
38
|
|
|
plugins : ['glsr_shortcode', 'wplink'] |
|
39
|
|
|
}); |
|
40
|
|
|
} |
|
41
|
|
|
setTimeout( function() { |
|
42
|
|
|
tinymce.execCommand( 'GLSR_Shortcode' ); |
|
|
|
|
|
|
43
|
|
|
}, 200 ); |
|
44
|
|
|
} |
|
45
|
|
|
else { |
|
46
|
|
|
// TinyMCE Editor |
|
47
|
|
|
tinymce.execCommand( 'GLSR_Shortcode' ); |
|
48
|
|
|
} |
|
49
|
|
|
setTimeout( function() { |
|
50
|
|
|
GLSR.shortcode.close(); |
|
|
|
|
|
|
51
|
|
|
}, 100 ); |
|
52
|
|
|
}; |
|
53
|
|
|
|
|
54
|
|
|
GLSR.shortcode.create = function( editor_id ) |
|
55
|
|
|
{ |
|
56
|
|
|
var editor = tinymce.get( editor_id ); |
|
|
|
|
|
|
57
|
|
|
if( !editor )return; |
|
|
|
|
|
|
58
|
|
|
var data = { |
|
59
|
|
|
action: site_reviews.action, |
|
|
|
|
|
|
60
|
|
|
request: { |
|
61
|
|
|
action: 'mce-shortcode', |
|
62
|
|
|
nonce: x( '#_glsr_nonce' ).val(), |
|
63
|
|
|
shortcode: GLSR.shortcode.current, |
|
|
|
|
|
|
64
|
|
|
}, |
|
65
|
|
|
}; |
|
66
|
|
|
x.post( site_reviews.ajaxurl, data, function( response ) |
|
|
|
|
|
|
67
|
|
|
{ |
|
68
|
|
|
if( !response.body )return; |
|
|
|
|
|
|
69
|
|
|
if( response.body.length === 0 ) { |
|
70
|
|
|
window.send_to_editor( '[' + response.shortcode + ']' ); |
|
71
|
|
|
GLSR.shortcode.destroy(); |
|
|
|
|
|
|
72
|
|
|
return; |
|
73
|
|
|
} |
|
74
|
|
|
var buttons = [{ |
|
75
|
|
|
text : response.ok, |
|
76
|
|
|
classes : 'btn glsr-btn primary', |
|
77
|
|
|
onclick : function() { |
|
78
|
|
|
var field, required, valid, win; |
|
79
|
|
|
// Get the top most window object |
|
80
|
|
|
win = editor.windowManager.getWindows()[0]; |
|
81
|
|
|
// Get the shortcode required attributes |
|
82
|
|
|
required = site_reviews.shortcodes[ GLSR.shortcode.current ]; |
|
|
|
|
|
|
83
|
|
|
valid = true; |
|
84
|
|
|
// Do some validation voodoo |
|
85
|
|
|
for( var id in required ) { |
|
86
|
|
|
if( !required.hasOwnProperty( id ) )continue; |
|
|
|
|
|
|
87
|
|
|
field = win.find( '#' + id )[0]; |
|
88
|
|
|
if( typeof field !== 'undefined' && field.state.data.value === '' ) { |
|
89
|
|
|
valid = false; |
|
90
|
|
|
alert( required[ id ] ); |
|
91
|
|
|
break; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
if( valid ) { |
|
95
|
|
|
win.submit(); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
},{ |
|
99
|
|
|
text : response.close, |
|
100
|
|
|
onclick : 'close' |
|
101
|
|
|
}]; |
|
102
|
|
|
var popup = { |
|
103
|
|
|
title : response.title, |
|
104
|
|
|
body : response.body, |
|
105
|
|
|
classes: 'glsr-mce-popup', |
|
106
|
|
|
minWidth: 320, |
|
107
|
|
|
buttons : buttons, |
|
108
|
|
|
onsubmit: function( e ) { |
|
109
|
|
|
var attributes = ''; |
|
110
|
|
|
var data = GLSR.shortcode.normalize( e.data ); |
|
|
|
|
|
|
111
|
|
|
for( var key in data ) { |
|
112
|
|
|
if( data.hasOwnProperty( key ) && data[ key ] !== '' ) { |
|
113
|
|
|
attributes += ' ' + key + '="' + data[ key ] + '"'; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
// Insert shortcode into the WP_Editor |
|
117
|
|
|
window.send_to_editor( '[' + response.shortcode + attributes + ']' ); |
|
118
|
|
|
}, |
|
119
|
|
|
onclose: function() { |
|
120
|
|
|
GLSR.shortcode.destroy(); |
|
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
}; |
|
123
|
|
|
// Change the buttons if server-side validation failed |
|
124
|
|
|
if( response.ok.constructor === Array ) { |
|
125
|
|
|
popup.buttons[0].text = response.ok[0]; |
|
126
|
|
|
popup.buttons[0].onclick = 'close'; |
|
127
|
|
|
delete popup.buttons[1]; |
|
128
|
|
|
} |
|
129
|
|
|
editor.windowManager.open( popup ); |
|
130
|
|
|
}); |
|
131
|
|
|
}; |
|
132
|
|
|
|
|
133
|
|
|
GLSR.shortcode.normalize = function( data ) |
|
134
|
|
|
{ |
|
135
|
|
|
var shortcodeHiddenFields = { |
|
136
|
|
|
'site_reviews' : ['author','date','excerpt','rating','response','title'], |
|
137
|
|
|
'site_reviews_form': ['email','name','terms','title'], |
|
138
|
|
|
'site_reviews_summary': ['bars','if_empty','rating','stars','summary'], |
|
139
|
|
|
}; |
|
140
|
|
|
var hide = []; |
|
141
|
|
|
for( var key in data ) { |
|
142
|
|
|
if( !data.hasOwnProperty( key ) )continue; |
|
|
|
|
|
|
143
|
|
|
if( shortcodeHiddenFields.hasOwnProperty( GLSR.shortcode.current ) ) { |
|
|
|
|
|
|
144
|
|
|
var value = ''; |
|
145
|
|
|
if( key.lastIndexOf( 'hide_', 0 ) === 0 ) { |
|
146
|
|
|
value = key.substring(5); |
|
147
|
|
|
} |
|
148
|
|
|
if( shortcodeHiddenFields[ GLSR.shortcode.current ].indexOf( value ) > -1 ) { |
|
149
|
|
|
if( data[ key ] ) { |
|
150
|
|
|
hide.push( value ); |
|
151
|
|
|
} |
|
152
|
|
|
delete data[ key ]; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
if( key === 'count' && !x.isNumeric( data[ key ] ) ) { |
|
|
|
|
|
|
156
|
|
|
data[ key ] = ''; |
|
157
|
|
|
} |
|
158
|
|
|
if( key === 'id' ) { |
|
159
|
|
|
data[ key ] = (+new Date()).toString(36); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
data.hide = hide.join( ',' ); |
|
163
|
|
|
return data; |
|
164
|
|
|
}; |
|
165
|
|
|
|
|
166
|
|
|
GLSR.shortcode.destroy = function() |
|
167
|
|
|
{ |
|
168
|
|
|
var tmp = x( '#scTemp' ); |
|
169
|
|
|
if( tmp.length ) { |
|
170
|
|
|
tinymce.get( 'scTemp' ).remove(); |
|
|
|
|
|
|
171
|
|
|
tmp.remove(); |
|
172
|
|
|
} |
|
173
|
|
|
}; |
|
174
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.